home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 22 xml and adonet / xmldemo / xmldatasetform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  10.3 KB  |  271 lines

  1. Imports System.Data
  2. Imports System.Data.OleDb
  3. Imports System.Data.SqlClient
  4. Imports System.IO
  5.  
  6. Public Class XmlDataSetForm
  7.     Inherits System.Windows.Forms.Form
  8.  
  9. #Region " Windows Form Designer generated code "
  10.  
  11.     Public Sub New()
  12.         MyBase.New()
  13.  
  14.         'This call is required by the Windows Form Designer.
  15.         InitializeComponent()
  16.  
  17.         'Add any initialization after the InitializeComponent() call
  18.  
  19.     End Sub
  20.  
  21.     'Form overrides dispose to clean up the component list.
  22.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  23.         If disposing Then
  24.             If Not (components Is Nothing) Then
  25.                 components.Dispose()
  26.             End If
  27.         End If
  28.         MyBase.Dispose(disposing)
  29.     End Sub
  30.  
  31.     'Required by the Windows Form Designer
  32.     Private components As System.ComponentModel.IContainer
  33.  
  34.     'NOTE: The following procedure is required by the Windows Form Designer
  35.     'It can be modified using the Windows Form Designer.  
  36.     'Do not modify it using the code editor.
  37.     Friend WithEvents txtOut As System.Windows.Forms.TextBox
  38.     Friend WithEvents btnWriteXml As System.Windows.Forms.Button
  39.     Friend WithEvents Button1 As System.Windows.Forms.Button
  40.     Friend WithEvents btnWriteDataSchema As System.Windows.Forms.Button
  41.     Friend WithEvents btnWriteSchema As System.Windows.Forms.Button
  42.     Friend WithEvents btnDiffGram As System.Windows.Forms.Button
  43.     Friend WithEvents btnGetChanges As System.Windows.Forms.Button
  44.     Friend WithEvents btnStronglyTyped As System.Windows.Forms.Button
  45.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  46.         Me.btnWriteXml = New System.Windows.Forms.Button()
  47.         Me.txtOut = New System.Windows.Forms.TextBox()
  48.         Me.Button1 = New System.Windows.Forms.Button()
  49.         Me.btnWriteDataSchema = New System.Windows.Forms.Button()
  50.         Me.btnWriteSchema = New System.Windows.Forms.Button()
  51.         Me.btnDiffGram = New System.Windows.Forms.Button()
  52.         Me.btnGetChanges = New System.Windows.Forms.Button()
  53.         Me.btnStronglyTyped = New System.Windows.Forms.Button()
  54.         Me.SuspendLayout()
  55.         '
  56.         'btnWriteXml
  57.         '
  58.         Me.btnWriteXml.Location = New System.Drawing.Point(8, 16)
  59.         Me.btnWriteXml.Name = "btnWriteXml"
  60.         Me.btnWriteXml.Size = New System.Drawing.Size(104, 40)
  61.         Me.btnWriteXml.TabIndex = 0
  62.         Me.btnWriteXml.Text = "WriteXml"
  63.         '
  64.         'txtOut
  65.         '
  66.         Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  67.                     Or System.Windows.Forms.AnchorStyles.Left) _
  68.                     Or System.Windows.Forms.AnchorStyles.Right)
  69.         Me.txtOut.Location = New System.Drawing.Point(120, 16)
  70.         Me.txtOut.Multiline = True
  71.         Me.txtOut.Name = "txtOut"
  72.         Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
  73.         Me.txtOut.Size = New System.Drawing.Size(520, 328)
  74.         Me.txtOut.TabIndex = 1
  75.         Me.txtOut.Text = ""
  76.         Me.txtOut.WordWrap = False
  77.         '
  78.         'Button1
  79.         '
  80.         Me.Button1.Location = New System.Drawing.Point(8, 64)
  81.         Me.Button1.Name = "Button1"
  82.         Me.Button1.Size = New System.Drawing.Size(104, 40)
  83.         Me.Button1.TabIndex = 2
  84.         Me.Button1.Text = "WriteXml (nested)"
  85.         '
  86.         'btnWriteDataSchema
  87.         '
  88.         Me.btnWriteDataSchema.Location = New System.Drawing.Point(8, 112)
  89.         Me.btnWriteDataSchema.Name = "btnWriteDataSchema"
  90.         Me.btnWriteDataSchema.Size = New System.Drawing.Size(104, 40)
  91.         Me.btnWriteDataSchema.TabIndex = 3
  92.         Me.btnWriteDataSchema.Text = "Write data and Schema"
  93.         '
  94.         'btnWriteSchema
  95.         '
  96.         Me.btnWriteSchema.Location = New System.Drawing.Point(8, 160)
  97.         Me.btnWriteSchema.Name = "btnWriteSchema"
  98.         Me.btnWriteSchema.Size = New System.Drawing.Size(104, 40)
  99.         Me.btnWriteSchema.TabIndex = 4
  100.         Me.btnWriteSchema.Text = "Write Schema only"
  101.         '
  102.         'btnDiffGram
  103.         '
  104.         Me.btnDiffGram.Location = New System.Drawing.Point(8, 208)
  105.         Me.btnDiffGram.Name = "btnDiffGram"
  106.         Me.btnDiffGram.Size = New System.Drawing.Size(104, 40)
  107.         Me.btnDiffGram.TabIndex = 5
  108.         Me.btnDiffGram.Text = "Write DiffGram"
  109.         '
  110.         'btnGetChanges
  111.         '
  112.         Me.btnGetChanges.Location = New System.Drawing.Point(8, 256)
  113.         Me.btnGetChanges.Name = "btnGetChanges"
  114.         Me.btnGetChanges.Size = New System.Drawing.Size(104, 40)
  115.         Me.btnGetChanges.TabIndex = 6
  116.         Me.btnGetChanges.Text = "GetChanges + DiffGram"
  117.         '
  118.         'btnStronglyTyped
  119.         '
  120.         Me.btnStronglyTyped.Location = New System.Drawing.Point(8, 304)
  121.         Me.btnStronglyTyped.Name = "btnStronglyTyped"
  122.         Me.btnStronglyTyped.Size = New System.Drawing.Size(104, 40)
  123.         Me.btnStronglyTyped.TabIndex = 7
  124.         Me.btnStronglyTyped.Text = "Strongly-typed DS"
  125.         '
  126.         'XmlDataSetForm
  127.         '
  128.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  129.         Me.ClientSize = New System.Drawing.Size(656, 357)
  130.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnStronglyTyped, Me.btnGetChanges, Me.btnDiffGram, Me.btnWriteSchema, Me.btnWriteDataSchema, Me.Button1, Me.txtOut, Me.btnWriteXml})
  131.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  132.         Me.Name = "XmlDataSetForm"
  133.         Me.Text = "XML and DataSets"
  134.         Me.ResumeLayout(False)
  135.  
  136.     End Sub
  137.  
  138. #End Region
  139.  
  140.     Dim ds As New DataSet()
  141.  
  142.     ' fill the DataSet from Pubs db
  143.  
  144.     Sub FillDataSet()
  145.         ds = New DataSet()
  146.         ' Fill the data set with data from two tables
  147.         Dim cn As New OleDbConnection(OledbPubsConnString)
  148.         cn.Open()
  149.         Dim daPub As New OleDbDataAdapter("SELECT * FROM Publishers", cn)
  150.         Dim daTit As New OleDbDataAdapter("SELECT * FROM Titles", cn)
  151.         daPub.Fill(ds, "Publishers")
  152.         daTit.Fill(ds, "Titles")
  153.         cn.Close()
  154.         ' Create a relationship between them.
  155.         ds.Relations.Add("PubTitles", ds.Tables("Publishers").Columns("pub_id"), ds.Tables("Titles").Columns("pub_id"))
  156.     End Sub
  157.  
  158.     ' Read the contents of a file.
  159.     Function GetTextFile(ByVal filename As String) As String
  160.         Dim sr As New StreamReader(filename)
  161.         GetTextFile = sr.ReadToEnd
  162.         sr.Close()
  163.     End Function
  164.  
  165.     ' write the dataset to XML
  166.  
  167.     Private Sub btnWriteXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteXml.Click
  168.         FillDataSet()
  169.         ds.WriteXml("ds.xml")
  170.         txtOut.Text = GetTextFile("ds.xml")
  171.         txtOut.Text = ds.GetXml
  172.     End Sub
  173.  
  174.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  175.         FillDataSet()
  176.         ds.Relations("PubTitles").Nested = True
  177.         'ds.WriteXml("ds.xml")
  178.         'txtOut.Text = GetTextFile("ds.xml")
  179.  
  180. #Const USE_TABLEMAPPINGS = True
  181.  
  182. #If USE_TABLEMAPPINGS Then
  183.         ' Set the ColumnMapping property for some columns.
  184.         ds.Tables("Publishers").Columns("pub_id").ColumnMapping = MappingType.Attribute
  185.         ds.Tables("Publishers").Columns("country").ColumnMapping = MappingType.Hidden
  186. #End If
  187.  
  188.         txtOut.Text = ds.GetXml
  189.     End Sub
  190.  
  191.     ' write the XML schema and the data
  192.  
  193.     Private Sub btnWriteDataSchema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteDataSchema.Click
  194.         FillDataSet()
  195.         ds.WriteXml("ds.xml", XmlWriteMode.WriteSchema)
  196.         txtOut.Text = GetTextFile("ds.xml")
  197.     End Sub
  198.  
  199.     ' write the XML schema only
  200.  
  201.     Private Sub btnWriteSchema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteSchema.Click
  202.         FillDataSet()
  203.         ds.WriteXmlSchema("ds.xsd")
  204.         txtOut.Text = GetTextFile("ds.xsd")
  205.     End Sub
  206.  
  207.     ' create a DiffGram
  208.  
  209.     Private Sub btnDiffGram_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiffGram.Click
  210.         FillDataSet()
  211.         ' Make some changes to the Titles table.
  212.         With ds.Tables("Titles")
  213.             ' Delete the first row.
  214.             .Rows(0).Delete()
  215.             ' Modify two fields in the second row.
  216.             .Rows(1)("price") = 49.99
  217.             .Rows(1)("advance") = 12300
  218.             ' Insert a new row.
  219.             Dim dr As DataRow = .NewRow
  220.             dr("title") = "Programming VB.NET"
  221.             dr("type") = "technical"
  222.             dr("price") = 59.99
  223.             .Rows.Add(dr)
  224.         End With
  225.  
  226.         ds.WriteXml("ds.xml", XmlWriteMode.DiffGram)
  227.         txtOut.Text = GetTextFile("ds.xml")
  228.     End Sub
  229.  
  230.     ' create a DataSet that contain only the changes
  231.  
  232.     Private Sub btnGetChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetChanges.Click
  233.         FillDataSet()
  234.         ' Make some changes to the Titles table.
  235.         With ds.Tables("Titles")
  236.             ' Delete the first row.
  237.             .Rows(0).Delete()
  238.             ' Modify two fields in the second row.
  239.             .Rows(1)("price") = 49.99
  240.             .Rows(1)("advance") = 12300
  241.             ' Insert a new row.
  242.             Dim dr As DataRow = .NewRow
  243.             dr("title") = "Programming VB.NET"
  244.             dr("type") = "technical"
  245.             dr("price") = 59.99
  246.             .Rows.Add(dr)
  247.         End With
  248.         Dim ds2 As DataSet = ds.GetChanges
  249.         ds2.WriteXml("ds.xml", XmlWriteMode.DiffGram)
  250.         txtOut.Text = GetTextFile("ds.xml")
  251.     End Sub
  252.  
  253.     ' use the Strongly-typed DataSet
  254.  
  255.     Private Sub btnStronglyTyped_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStronglyTyped.Click
  256.         Dim pubsDs As New PubsDataSet()
  257.         ' Add a new row to the Publishers table.
  258.         Dim pubsRow As PubsDataSet.PublishersRow = pubsDs.Publishers.NewPublishersRow
  259.         pubsRow.pub_id = "1234"
  260.         pubsRow.pub_name = "VB2TheMax"
  261.         pubsRow.city = "Bari"
  262.         pubsRow.country = "Italy"
  263.         pubsDs.Publishers.AddPublishersRow(pubsRow)
  264.  
  265.         If pubsDs.Publishers(0).IscityNull Then
  266.             pubsDs.Publishers(0).SetcountryNull()
  267.         End If
  268.     End Sub
  269. End Class
  270.  
  271.